﻿using System;
using System.Windows.Forms;
using UniMana.Utils.Helper;
using UniMana.Utils.Configs;
using UniMana.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmServerConfig : DevExpress.XtraEditors.XtraForm
    {
        
       
        public FrmServerConfig()
        {
            InitializeComponent();
           
        }
        private void FrmServerConfig_Load(object sender, EventArgs e)
        {
            cmbAuthentication.Properties.Items.Add("Windows Authenticaion");
            cmbAuthentication.Properties.Items.Add("SQL Server Authenticaion");
            

            ClsRegistryOperator clsRegOperator = new ClsRegistryOperator();

            if (clsRegOperator.LoadInfoRegistry("RemConnection".Encrypt()) != "")
            {
                ckeSaveInfo.Checked = bool.Parse(clsRegOperator.LoadInfoRegistry("RemConnection".Encrypt()));
            }
            if (ckeSaveInfo.Checked == true)
            {
                cmbServer.Text = clsRegOperator.LoadInfoRegistry("DataSource".Encrypt());
                txtDatabase.Text = clsRegOperator.LoadInfoRegistry("DataBase".Encrypt());

                //if (clsRegOperator.LoadInfoRegistry("Authentication".Encrypt()) == "Windows Authenticaion")
                //{
                //    cmbAuthentication.SelectedIndex = 0;
                //}
                //else
                //{
                    cmbAuthentication.SelectedIndex = 1;

                //}
                cmbUserID.Text = clsRegOperator.LoadInfoRegistry("User ID".Encrypt());
                txtPassword.Text = clsRegOperator.LoadInfoRegistry("Password".Encrypt());
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            
        }

        private void cmbServer_EditValueChanged(object sender, EventArgs e)
        {
           

        }

        //private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (cmbAuthentication.SelectedIndex == 0)
        //    {
        //        cmbUserID.Enabled = false;
        //        txtPassword.Enabled = false;
        //    }

        //    if (cmbAuthentication.SelectedIndex == 1)
        //    {
        //        cmbUserID.Enabled = true;
        //        txtPassword.Enabled = true;
        //    }

        //}

        private void btnOK_Click(object sender, EventArgs e)
        {
            string nstrDataSource ="DataSource";
            string nstrDataBase = "DataBase";
            string nstrPassword = "Password";
            string nstrAuthenticaion = "Authentication";
            string nstrUserID = "User ID";
            string nstrRemConnection = "RemConnection";
            ClsRegistryOperator clsRegOperator = new ClsRegistryOperator();

            clsRegOperator.SaveInfoRegistry(nstrRemConnection.Encrypt(), ckeSaveInfo.Checked.ToString());

            if (cmbAuthentication.SelectedIndex == 1)
            {
                if (cmbUserID.Text != "" && txtPassword.Text != "")
                {
                    clsRegOperator.SaveInfoRegistry(nstrPassword.Encrypt(), txtPassword.Text);
                    clsRegOperator.SaveInfoRegistry(nstrUserID.Encrypt(), cmbUserID.Text);
                }
                else
                {
                    MessageBox.Show("Tài khoản và mật khẩu không được rỗng");
                    return;
                }
            }

            clsRegOperator.SaveInfoRegistry(nstrDataSource.Encrypt(), cmbServer.Text);
            clsRegOperator.SaveInfoRegistry(nstrAuthenticaion.Encrypt(), cmbAuthentication.Text);
            clsRegOperator.SaveInfoRegistry(nstrDataBase.Encrypt(), txtDatabase.Text);

            ResetConnectionString();
            Close();
        }

        private void ResetConnectionString()
        {
            ServerConfig.mDataSource = cmbServer.Text;
            ServerConfig.mDataBase = txtDatabase.Text;
            ServerConfig.mUser = cmbUserID.Text;
            ServerConfig.mPassword = txtPassword.Text;
        }

        private void cmbUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (SysGlobalVar.g_sta_boolLoginSuccess == false)
            {
                Application.Exit();
            }
            else
            {
                Close();
            }
        }
    }
}